// FrogSplitDialog.cpp : Implementation of CFrogSplitDialog

#include "stdafx.h"
#include "FrogSplitDialog.h"
#include "FrogMessageBox.h"

// CFrogSplitDialog
CFrogSplitDialog::CFrogSplitDialog(HICON hIcon):
CFrogProgressReportDialog(hIcon),
m_SpiltSize(1024 * 1024)
{
}

LRESULT CFrogSplitDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CFrogBaseDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);

	HWND comboSize = GetDlgItem(IDC_COMBO_SIZE);

	if (comboSize != NULL)
	{
		::SendMessage(comboSize,(UINT)CB_ADDSTRING, 0, (LPARAM)L"3.5\": 1,457,664 Bytes");
		::SendMessage(comboSize,(UINT)CB_ADDSTRING, 0, (LPARAM)L"Zip100: 9,8078 Kilobytes");
		::SendMessage(comboSize,(UINT)CB_ADDSTRING, 0, (LPARAM)L"CD: 700 Megabytes");

		::SendMessage(comboSize,(UINT)CB_ADDSTRING, 0, (LPARAM)L"1 Megabyte: 1,048,576 Bytes");
		::SendMessage(comboSize,(UINT)CB_ADDSTRING, 0, (LPARAM)L"5 Megabytes: 5,242,880 Bytes");
		::SendMessage(comboSize,(UINT)CB_ADDSTRING, 0, (LPARAM)L"10 Megabytes: 10,485,760 Bytes");

		// Selects the first item
		::SendMessage(comboSize,(UINT)CB_SETCURSEL, (WPARAM) 0 , 0);
		::SetFocus(comboSize);
	}

	HWND comboSizeCustom = GetDlgItem(IDC_COMBO_SPLIT_UNITS);

	if (comboSizeCustom != NULL)
	{
		::SendMessage(comboSizeCustom,(UINT)CB_ADDSTRING, 0, (LPARAM)L"Bytes");
		::SendMessage(comboSizeCustom,(UINT)CB_ADDSTRING, 0, (LPARAM)L"Kilobytes");
		::SendMessage(comboSizeCustom,(UINT)CB_ADDSTRING, 0, (LPARAM)L"Megabytes");

		// Selects the last item
		::SendMessage(comboSizeCustom,(UINT)CB_SETCURSEL, (WPARAM)2 , 0);
	}

	HWND radioPredefined = GetDlgItem(IDC_RADIO_SPLIT_PREDEFINED);
	HWND radioCustom = GetDlgItem(IDC_RADIO_SPLIT_CUSTOM);

	CheckDlgButton(IDC_RADIO_SPLIT_PREDEFINED, BST_CHECKED);
	CheckDlgButton(IDC_RADIO_SPLIT_CUSTOM, BST_UNCHECKED);

	HWND spinHwndW = GetDlgItem(IDC_SPIN_SPLIT);
	::SendMessage(spinHwndW, (UINT) UDM_SETRANGE, (WPARAM)0, (LPARAM)MAKELONG((short)UD_MAXVAL, (short)1));

	::SetWindowText(GetDlgItem(IDC_EDIT_SPLIT_SIZE), L"1");

	::EnableWindow(GetDlgItem(IDC_COMBO_SIZE), TRUE);

	::EnableWindow(GetDlgItem(IDC_EDIT_SPLIT_SIZE), FALSE);
	::EnableWindow(GetDlgItem(IDC_SPIN_SPLIT), FALSE);
	::EnableWindow(GetDlgItem(IDC_STATIC_UNITS), FALSE);
	::EnableWindow(GetDlgItem(IDC_COMBO_SPLIT_UNITS), FALSE);

	return 0;
}

LRESULT CFrogSplitDialog::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	bHandled = TRUE;
	// TODO: Add your control notification handler code here
	m_SpiltSize = 1024 * 1024;

	HWND buttonPredefined = GetDlgItem(IDC_RADIO_SPLIT_PREDEFINED);
	LRESULT lResult = SendMessage(buttonPredefined, (UINT)BM_GETCHECK, 0, 0);

	if (lResult == BST_CHECKED)
	{
		HWND editBoxWindowHandle = GetDlgItem(IDC_COMBO_SIZE);

		if (editBoxWindowHandle != NULL)
		{
			lResult = SendMessage(editBoxWindowHandle,(UINT)CB_GETCURSEL, 0 , 0);

			if (lResult == CB_ERR)
			{
				wchar_t buffer[1024];
				::GetWindowText(editBoxWindowHandle, buffer, 1024);
				m_SpiltSize = _wtoi(buffer);

				if (m_SpiltSize <= 0)
				{
					FrogMessageBox(m_hWnd, L"Error Converting Size, Please Enter a Valid Size in Bytes", L"FilerFrog - Split", 0);
					return TRUE; 
				}
			}
			else
			{
				switch (lResult)
				{
				case 0:
					{
						m_SpiltSize = 1457664;
					}
					break;
				case 1:
					{
						m_SpiltSize = 100431872;

					}
					break;
				case 2:
					{
						m_SpiltSize = 734003200;
					}
					break;
				case 3:
					{
						m_SpiltSize = 1048576;
					}
					break;
				case 4:
					{
						m_SpiltSize = 5242880;
					}
					break;
				case 5:
					{
						m_SpiltSize = 10485760;
					}
					break;
				}
			}
		}
	}
	else // Use Custom Values
	{
		HWND editBoxWindowHandle = GetDlgItem(IDC_EDIT_SPLIT_SIZE);

		if (editBoxWindowHandle != NULL)
		{
			lResult = SendMessage(GetDlgItem(IDC_COMBO_SPLIT_UNITS) ,(UINT)CB_GETCURSEL, 0 , 0);

			wchar_t buffer[1024];
			::GetWindowText(editBoxWindowHandle, buffer, 1024);
			m_SpiltSize = _wtoi(buffer);

			if (m_SpiltSize <= 0)
			{
				FrogMessageBox(m_hWnd, L"Error Converting Size, Please Enter a Valid Size in Bytes", L"FilerFrog - Split", 0);
				return TRUE; 
			}

			switch (lResult)
			{
			case 0:
				{
					// Bytes
					m_SpiltSize = m_SpiltSize;
				}
				break;
			case 1:
				{
					// Kilo Bytes
					m_SpiltSize = 1024 * m_SpiltSize;
				}
				break;
			case 2:
				{
					// Mega Bytes
					m_SpiltSize = 1048576 * m_SpiltSize;
				}
				break;
			}						
		}
	}	

	RunAsyncCommandWithProgressBar(false);

	EndDialog(wID);
	return 0;
}

void CFrogSplitDialog::ExecuteCommand()
{
	m_FilesRefactor.SplitFile(m_LastResult, m_FilesList.front(), m_SpiltSize);
}

LRESULT CFrogSplitDialog::OnRadioSplitPredefined(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	HWND button = GetDlgItem(IDC_RADIO_SPLIT_PREDEFINED);
	LRESULT lResult = SendMessage(button, (UINT)BM_GETCHECK, 0, 0);

	if (lResult == BST_CHECKED)
	{
		::EnableWindow(GetDlgItem(IDC_COMBO_SIZE), TRUE);
		::EnableWindow(GetDlgItem(IDC_EDIT_SPLIT_SIZE), FALSE);
		::EnableWindow(GetDlgItem(IDC_SPIN_SPLIT), FALSE);
		::EnableWindow(GetDlgItem(IDC_STATIC_UNITS), FALSE);
		::EnableWindow(GetDlgItem(IDC_COMBO_SPLIT_UNITS), FALSE);

		CheckDlgButton(IDC_RADIO_SPLIT_CUSTOM, BST_UNCHECKED);
	}

	return 0;
}

LRESULT CFrogSplitDialog::OnRadioSplitCustom(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	HWND button = GetDlgItem(IDC_RADIO_SPLIT_CUSTOM);
	LRESULT lResult = SendMessage(button, (UINT)BM_GETCHECK, 0, 0);

	if (lResult == BST_CHECKED)
	{
		::EnableWindow(GetDlgItem(IDC_COMBO_SIZE), FALSE);
		::EnableWindow(GetDlgItem(IDC_EDIT_SPLIT_SIZE), TRUE);
		::EnableWindow(GetDlgItem(IDC_SPIN_SPLIT), TRUE);
		::EnableWindow(GetDlgItem(IDC_STATIC_UNITS), TRUE);
		::EnableWindow(GetDlgItem(IDC_COMBO_SPLIT_UNITS), TRUE);

		CheckDlgButton(IDC_RADIO_SPLIT_PREDEFINED, BST_UNCHECKED);
	}

	return 0;
}